<?php
class Admin_ContentController extends Zend_Controller_Action
{
	public function init()
    {
  
	}
	
	public function indexAction()
    {
		$storage = new Zend_Auth_Storage_Session();
        $data = $storage->read();
        if(!$data){
            $this->_redirect('admin/auth/login');
        }
        
	    $this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/forms/LoginForm.php");
		$form = new AML_Form_LoginForm();		
        $this->view->form = $form;
	 }
	 
	public function mediaAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		$request = $this->getRequest();
		$stat = $request->getParam('STAT');
		if($stat == "add")
		{
			$this->view->msg  = "<span class='success'>Promo banner has been successfully added.</span>";
		} else if ($stat == "update") {
			$this->view->msg  = "<span class='success'>Promo banner has been successfully updated.</span>";
		} else if ($stat == "del") {
			$this->view->msg  = "<span class='success'>Promo banner has been successfully deleted.</span>";
		}
		
		$main_dir = "images/media";
		//$dir_structure = $this->get_subdir_files($dir);	
		//$this->view->dir_structure = $dir_structure;
		
		$dirs = scandir($main_dir);
       	$this->view->dirs = $dirs;
       	$this->view->main_dir = $main_dir;
	}
	 
	/**
	* Private function to get the file extention
	*
	* @param String $str
	* @return String
	*/
	 
	private function getExtension($str) {	
		$i = strrpos($str,".");
		if (!$i) { return ""; } 
		
		$l = strlen($str) - $i;
		$ext = substr($str,$i+1,$l);
		return $ext;
	}
	
	/**
	 * Public function for file upload
	 *
	 */
	 
	public function uploaderAction()
	{
		//$this->_helper->layout()->disableLayout();
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/models/Media.php");
		Zend_Loader::loadFile("../application/admin/models/ResizeImage.php");
	 	
	 	$modMedia = new Media();
	 	$post_arr = $this->getRequest()->getPost();
	 	
	 	define ("MAX_SIZE","1500");
	 	$image_path = "images/media/promo_banners";
	 	$image = $_FILES['bannerimage']['name'];
	 	
	 	$banner_id = $post_arr['txtPBannerId'];
    	
    	$errors = 0;
  		if ($image) 
  		{
  			$filename = stripslashes($_FILES['bannerimage']['name']);
        	$extension = $this->getExtension($filename);
  			$extension = strtolower($extension);
 			if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
  			{
				echo 'Unknown Image extension';
				$errors=1;
  			} else {
   				$size=filesize($_FILES['bannerimage']['tmp_name']); 
				if ($size > MAX_SIZE*3000)
				{
					echo "You have exceeded the size limit";
					$errors=1;
				} else {
					if(move_uploaded_file($_FILES['bannerimage']['tmp_name'], $image_path . "/" . $_FILES['bannerimage']['name'])) {
						
						//resizing image - promo banner
						$clsResizeImg = new ImageResize($image_path . "/" . $_FILES['bannerimage']['name']);		
						$clsResizeImg -> resizeImage(800, 248, 'crop');	
						$clsResizeImg -> saveImage($image_path . "/" . $_FILES['bannerimage']['name'], 100);
					}
				}							
			}
		}
		
		if($errors == 0 && $banner_id == "") 
	 	{
	   		// add banner details to the database
	 		$add_banner = $modMedia->addPromoBanner($post_arr, $_FILES['bannerimage']['name']);	  		
	 	} else {
	 		$update_banner = $modMedia->updatePromoBanner($post_arr);	  
	 	}
	} 
	
	/**
	 * get sub dir and files in a specific DIR
	 *
	 * @param String $dir
	 * @return array
	 */
	
	private function get_subdir_files($main_dir) {
	    $dirs = scandir($main_dir);
	    
	    foreach($dirs as $dir) {
	    	
			if ($dir === '.' || $dir === '..' || $dir === 'Thumbs.db') {
                continue; 
           	} else {
            	
            	$img_path = $main_dir . "/" . $dir;
            	if(is_dir($img_path)) // check whether image or directory
            	{
            		$file_icon = "<img src='/images/admin/folder_icon.png' />"; 
            		$toolbar = "";          		
				} else {
					$file_icon = "<img src='/" . $main_dir . "/" . $dir . "' width='100' height='45' />";
					$toolbar = "<a href='javascript: void(0);' onclick='editPromoBanner(\"" . $img_path . "\")'><img src='/images/admin/edit.gif' width='15' height='15' /></a>&nbsp;&nbsp;&nbsp;<a href='javascript: void(0);' onclick='deleteImage(\"" . $img_path . "\")'><img src='/images/admin/disable.gif' width='15' height='15' /></a>";
				}
				
				// file_icon - change the folder icon if $dir is image
				
            	$list_dirs = "<div style='float: left;'>" . $file_icon . "<br /><p class='txt_size11' style='text-align: center;'>" . $dir . "</p><p style='text-align: center;'>" . $toolbar . "</p></div>";
	            
	            echo "<a href='javascript: void(0);' onclick='showBannerDir(\"" . $dir . "\")'>" . $list_dirs . "</a>";
        	}
	    }   
	    //return $result;
	} 
	
	public function showbannerAction()
	{
		$post_arr = $this->getRequest()->getPost();
		
		switch ($post_arr['mediatype'])
		{
			case "media" :
			//default :
				$dir = "images/media";
				$this->get_subdir_files($dir);
				break;
				
			case "promo_banners" :
				$dir = "images/media/promo_banners";
				$this->get_subdir_files($dir);
				break;
				
		}
		exit;		
	}
	
	public function delimageAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Media.php");
		$modMedia = new Media();
		$post_arr = $this->getRequest()->getPost();
		$imgpath = $post_arr['imgpath'];
		
		if(unlink($imgpath))
		{
			$modMedia->delImage($imgpath);
		}
	}
	
	public function changeimageAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Media.php");
		$modMedia = new Media();
		$post_arr = $this->getRequest()->getPost();
		$imgpath = $post_arr['imgpath'];
		
		$data_arr = $modMedia->getImageDetails($imgpath);
		echo $data_arr['pbanner_id'] . "|" . $data_arr['pbanner_title'] . "|" . $data_arr['pbanner_desc'] . "|" . $data_arr['pbanner_url'] . "|" . $data_arr['is_active'] . "|" . $data_arr['pbanner_filename'];		
	}
}
